package com.yy.cloud.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义的GoodsId的局部过滤器；
 *
 * @date 2024/5/11
 */
@Component
public class GoodsIdGatewayFilterFactory extends AbstractGatewayFilterFactory<GoodsIdGatewayFilterFactory.Config> {

    private static final String BAD_MESSAGE = "BAD REQUEST";

    public GoodsIdGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        // 定义配置文件中的参数项(最大值和最小值)
        return Arrays.asList("minValue", "maxValue");
    }

    @Override
    public GatewayFilter apply(GoodsIdGatewayFilterFactory.Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                String goodsId = exchange.getRequest().getQueryParams().getFirst("goodsId");
                if (!StringUtils.isEmpty(goodsId)) {
                    int goodsIdNum = Integer.parseInt(goodsId);
                    // 如果goodsId的取值范围在设置的最小值和最大值之间则放行
                    if (goodsIdNum >= config.minValue && goodsIdNum <= config.maxValue) {
                        return chain.filter(exchange);
                    }else {
                        // 不符合条件,返回错误的提示信息，不进行后续的路由
                        byte[] bytes = BAD_MESSAGE.getBytes(StandardCharsets.UTF_8);
                        DataBuffer wrap = exchange.getResponse().bufferFactory().wrap(bytes);
                        exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
                        return exchange.getResponse().writeWith(Flux.just(wrap));
                    }
                }

                byte[] bytes = "goodsId不能为空".getBytes(StandardCharsets.UTF_8);
                DataBuffer wrap = exchange.getResponse().bufferFactory().wrap(bytes);
                exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
                return exchange.getResponse().writeWith(Flux.just(wrap));
            }
        };
    }


    public static class Config {
        private int minValue;
        private int maxValue;

        public int getMinValue() {
            return minValue;
        }

        public void setMinValue(int minValue) {
            this.minValue = minValue;
        }

        public int getMaxValue() {
            return maxValue;
        }

        public void setMaxValue(int maxValue) {
            this.maxValue = maxValue;
        }
    }
}
